﻿// +---------------------------------------------------------------------------+
// +  Datei: Form1.cs                                                          |
// +  Autoren: Axel Winter (axelwinter@gmx.de)                                 |
// +           Claus Krause (clauskrause@gmx.de)                               |
// +  Datum: 14. Juli 2011                                                     |
// +---------------------------------------------------------------------------+
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace D5
{
    public partial class Form1 : Form
    {
        // --- Deklarationen ---------------------------------------------------
        private bool mouseDown;
        int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
        int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
        int Breite = Screen.PrimaryScreen.Bounds.Width;
        int Hoehe = Screen.PrimaryScreen.Bounds.Height;
        private readonly List<Point> list;
        private readonly Bitmap bild;
        private int stiftbreite = 4;
        private int stiftfarbe = 1;
        string tempPfad = Environment.GetEnvironmentVariable("Temp");
        
        // --- Konstruktor -----------------------------------------------------
        public Form1()
        {
            MessageBox.Show("Programm starten?");

            InitializeComponent();

            this.FormBorderStyle = FormBorderStyle.None;

            this.Left = 0;
            this.Top = 0;

            this.Width = screenWidth;
            this.Height = screenHeight;

            pictureBox1.Width = screenWidth;
            pictureBox1.Height = screenHeight;

            this.list = new List<Point>();

            string Dateiname = tempPfad + "/tmp.bmp"; // Dateinamen erzeugen
            this.bild = new Bitmap(Breite, Hoehe);
            Graphics grafik = Graphics.FromImage(bild);
            grafik.CopyFromScreen(0, 0, 0, 0, new Size(Breite, Hoehe), CopyPixelOperation.SourceCopy);
            bild.Save(Dateiname, ImageFormat.Bmp);
            pictureBox1.Image = bild;
        }

        // +-------------------------------------------------------------------+
        // +  Methode Draw                                                     |
        // +-------------------------------------------------------------------+
        private void Draw(Graphics g)
        {
            if (list.Count > 0)
            {
                byte[] bs = new byte[list.Count];       // 
                                                        //
                bs[0] = (byte)PathPointType.Start;      // ANSCHAUEN
                                                        //
                for (int i = 1; i < list.Count; i++)    //
                    bs[i] = (byte)PathPointType.Line;   //

                // --- Stift wählen --------------------------------------------
                switch (stiftfarbe)
                {
                    case 1:
                        using (Pen p = new Pen(Color.Black, stiftbreite))
                            g.DrawPath(p, new System.Drawing.Drawing2D.GraphicsPath(list.ToArray(), bs));
                        break;
                    case 2:
                        using (Pen p = new Pen(Color.Red, 10))
                            g.DrawPath(p, new System.Drawing.Drawing2D.GraphicsPath(list.ToArray(), bs));
                        break;
                    case 3:
                        using (Pen p = new Pen(Color.Blue, stiftbreite))
                            g.DrawPath(p, new System.Drawing.Drawing2D.GraphicsPath(list.ToArray(), bs));
                        break;
                    default:
                        using (Pen p = new Pen(Color.Black, stiftbreite))
                            g.DrawPath(p, new System.Drawing.Drawing2D.GraphicsPath(list.ToArray(), bs));
                        break;
                }
            }
        }

        private void schließenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            list.Add(new Point(e.X, e.Y));
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                mouseDown = false;

                using (Graphics g = Graphics.FromImage(bild))
                {
                    Draw(g);
                    g.Flush();
                }

                list.Clear();
                pictureBox1.Invalidate();
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
            {
                list.Add(new Point(e.X, e.Y));
                pictureBox1.Invalidate();
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (mouseDown)
            {
                Draw(e.Graphics);
            }

            // Create string to draw.
            String drawString = "Sample Text";

            // Create font and brush.
            Font drawFont = new Font("Arial", 32);
            SolidBrush drawBrush = new SolidBrush(Color.Black);

            // Create point for upper-left corner of drawing.
            PointF drawPoint = new PointF(150.0F, 150.0F);

            // Draw string to screen.
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
        }

        private void saveScreen_Click(object sender, EventArgs e)
        {
            // --- Die Höhe der Taskleiste ermitteln ---------------------------
            int taskleiste = Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height;

            Bitmap Screenshot = new Bitmap(Breite, Hoehe - taskleiste);
            Graphics graf = Graphics.FromImage(Screenshot);
            graf.CopyFromScreen(0, 0, 0, 0, new Size(Breite, Hoehe - taskleiste), CopyPixelOperation.SourceCopy);

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "PNG Bild|*.png|JPEG Bild|*.jpg|Bitmap Bild|*.bmp";
            saveFileDialog1.Title = "Bild speichern";
            saveFileDialog1.ShowDialog();

            // Eingabestring muss vorhanden sein
            if (saveFileDialog1.FileName != "")
            {
                string Dateiname = saveFileDialog1.FileName;

                switch (saveFileDialog1.FilterIndex)
                {
                    case 1:
                        Screenshot.Save(Dateiname, System.Drawing.Imaging.ImageFormat.Png);
                        break;

                    case 2:
                        Screenshot.Save(Dateiname, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;

                    case 3:
                        Screenshot.Save(Dateiname, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
                }
                Screenshot.Dispose(); // Ressourcen freigeben
            }
        }

        // --- Stiftfarbe Schwarz zuweisen -------------------------------------
        private void schwarzToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stiftfarbe = 1;
        }

        // --- Stiftfarbe Rot zuweisen -----------------------------------------
        private void rotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stiftfarbe = 2;
        }

        // --- Stiftfarbe Blau zuweisen ----------------------------------------
        private void blauToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stiftfarbe = 3;
        }

        // --- schmalen Stift zuweisen -----------------------------------------
        private void pxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stiftbreite = 2;
        }

        // --- normalen Stift zuweisen -----------------------------------------
        private void pxToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            stiftbreite = 4;
        }

        // --- breiten Stift zuweisen ------------------------------------------
        private void pxToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            stiftbreite = 6;
        }
    }
}
